package com.qk.controller;
import com.qk.Response.PageResult;
import com.qk.dto.DeptPageDTO;
import com.qk.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.qk.entity.Dept;
import com.qk.Response.Result;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {

    @Autowired
    private DeptService deptService;
    /**
     * 添加部门
     * @param dept
     * @return
     */
    @PostMapping
    public Result addDept(@RequestBody Dept dept ){
        log.info("添加部门：{}",dept);
        deptService.addDept(dept);
        return Result.success();
    }
    /*
    部门列表分页查询
     */
    @GetMapping
    public Result selectPageDept(DeptPageDTO deptPageDTO){
        log.info("部门列表分页查询：{}",deptPageDTO);
        PageResult<Dept> pageResult=deptService.selectPageDept(deptPageDTO);
        return Result.success(pageResult);
    }
    /*
    更新部门
    先根据id查询部门，再更新
     */
    @GetMapping("/{id}")
    public Result selectDeptById(@PathVariable Integer id){
        log.info("查询部门：{}",id);
        Dept dept=deptService.selectDeptById(id);
        return Result.success(dept);
    }
    @PutMapping
    public Result updateDept(@RequestBody Dept dept){
        log.info("更新部门：{}",dept);
        deptService.updateDept(dept);
        return Result.success();
    }
    /**
     * 删除部门
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteDeptById(@PathVariable Integer id){
        log.info("删除部门：{}",id);
        deptService.deleteDeptById(id);
        return Result.success();
    }
    /*
    查询全部部门
     */
    @GetMapping("/list")
    public Result selectAllDept(){
        log.info("查询全部部门");
        List<Dept> depts=deptService.selectAllDept();
        return Result.success(depts);
    }

}